<?php
namespace app\applet\model;

use app\manage\model\AdminUser;
use think\Db;
use think\Model;

class NetMeter extends Model {
    protected $table = "net_meter";
    public function __construct($data = [])
    {
//        parent::__construct($data);
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($id, $data) {
        $data = Db::name($this->table)
            ->where(['id'=>$id])
            ->update($data);
        return $data;
    }

    /**
     * 获取全部列表
     * @param $admin_id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($admin_id, $status, $page, $keyword) {
        $where = [];
        if ($admin_id != 1) {
            $model = new AdminUser();
            $detail = $model->detail($admin_id);
            if ($detail['admin_sign'] != 1) {
                if ($admin_id !== null && $admin_id !== "") {
                    $where['admin_id'] = $admin_id;
                }
            }

        }
        if ($status !== null && $status !== "") {
            $where['pay'] = $status;
        }

        $data['data'] = Db::name($this->table)
            ->field('id, house_id, type, tung_num, unit_num, layer_num, room_number, pay, pay_sum, pay_time, tenant_name, tenant_tel')
            ->where($where)
            ->where('tenant_name|tenant_tel', 'like', '%' . $keyword . '%')
            ->page($page, 10)
            ->order('tung_num asc, unit_num asc, layer_num asc, room_number asc')
            ->select();
        $data['count'] = Db::name($this->table)
            ->where($where)
            ->where('tenant_name|tenant_tel', 'like', '%' . $keyword . '%')
            ->count();

        return $data;
    }
}